package cn.tedu.charging.order.dao.repository.impl;

import cn.tedu.charging.order.dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

//@Repository("orderRepository")
@Component
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;
    @Autowired
    private OrderFailMapper orderFailMapper;

    public OrderRepositoryImpl(OrderSuccessMapper orderSuccessMapper) {
        this.orderSuccessMapper = orderSuccessMapper;
    }

    @Override
    public ChargingBillSuccessPO getSuccessOrder(String orderNo) {
        // 正确的用法
        QueryWrapper<ChargingBillSuccessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id", orderNo);
        return orderSuccessMapper.selectOne(queryWrapper);

    }

    @Override
    public ChargingBillFailPO getFailOrder(String orderNo) {
        QueryWrapper<ChargingBillFailPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id", orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }


    @Override
    public void saveFailOrder(ChargingBillFailPO newChargingBillFailPO) {
        orderFailMapper.insert(newChargingBillFailPO);
    }

    @Override
    public void updateDeviceInfo(String orderNo, Integer gunId) {
        // 更新目标值
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setGunId(gunId);

        // updateWrapper 更新条件 where bill_id = orderNo
        QueryWrapper<ChargingBillSuccessPO> updateWrapper = new QueryWrapper<>();
        // bill_id = orderNo
        updateWrapper.eq("bill_id", orderNo);
        orderSuccessMapper.update(chargingBillSuccessPO, updateWrapper);
    }
}
